

class LanguageController extends BaseController{
    def beforeInterceptor = [action:this.highestAuth, except:['changeLanguage']]
    
    def index = { redirect(action:list,params:params) }

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [delete:'POST', save:'POST', update:'POST']

    def list = {
        params.max = Math.min( params.max ? params.max.toInteger() : 10,  100)
        [ languageInstanceList: Language.list( params ), languageInstanceTotal: Language.count() ]
    }

    def show = {
        def languageInstance = Language.get( params.id )

        if(!languageInstance) {
            flash.message = "Language not found with id ${params.id}"
            redirect(action:list)
        }
        else { return [ languageInstance : languageInstance ] }
    }

    def delete = {
        def languageInstance = Language.get( params.id )
        if(languageInstance) {
            try {
                languageInstance.delete(flush:true)
                flash.message = "Language ${params.id} deleted"
                redirect(action:list)
            }
            catch(org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "Language ${params.id} could not be deleted"
                redirect(action:show,id:params.id)
            }
        }
        else {
            flash.message = "Language not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def edit = {
        def languageInstance = Language.get( params.id )

        if(!languageInstance) {
            flash.message = "Language not found with id ${params.id}"
            redirect(action:list)
        }
        else {
            return [ languageInstance : languageInstance ]
        }
    }

    def update = {
        def languageInstance = Language.get( params.id )
        if(languageInstance) {
            if(params.version) {
                def version = params.version.toLong()
                if(languageInstance.version > version) {
                    
                    languageInstance.errors.rejectValue("version", "language.optimistic.locking.failure", "Another user has updated this Language while you were editing.")
                    render(view:'edit',model:[languageInstance:languageInstance])
                    return
                }
            }
            languageInstance.properties = params
            if(!languageInstance.hasErrors() && languageInstance.save()) {
                flash.message = "Language ${params.id} updated"
                redirect(action:show,id:languageInstance.id)
            }
            else {
                render(view:'edit',model:[languageInstance:languageInstance])
            }
        }
        else {
            flash.message = "Language not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def create = {
        def languageInstance = new Language()
        languageInstance.properties = params
        return ['languageInstance':languageInstance]
    }

    def save = {
        def languageInstance = new Language(params)
        if(!languageInstance.hasErrors() && languageInstance.save()) {
            flash.message = "Language ${languageInstance.id} created"
            redirect(action:show,id:languageInstance.id)
        }
        else {
            render(view:'create',model:[languageInstance:languageInstance])
        }
    }
    def changeLanguage = {

  
        if((session.language==null)||(!session.language.equalsIgnoreCase("en"))){
            session.language="en"
        }
        else{
            session.language="sv"
        }

        //for first page where there are no controller or view
        
      if(params.currentController.equals("null")){
          redirect(controller:'project', view:'list')
      }

        //if there is no ID
      if(params.idProject.equals("null")){
           redirect(controller:params.currentController,action:params.currentView)
       }
       else{
           redirect(controller:params.currentController,action:params.currentView,id:params.idProject)

    }}}
